package cn.edu.tzc.controller;


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.edu.tzc.pojo.Card;
import cn.edu.tzc.pojo.MyUser;
import cn.edu.tzc.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/card")
public class CardController {
    @Autowired
    private CardService cardService;
    /**
     * 鏉冮檺鎺у埗
     */
    @ModelAttribute
    public void checkLogin(HttpSession session) throws NoLoginException{
        if(session.getAttribute("userLogin") == null) {
            throw new NoLoginException();
        }
    }
    /**
     * 鏌ヨ銆佷慨鏀规煡璇€�佸垹闄ゆ煡璇�
     */
    @RequestMapping("/selectAllCardsByPage")
    public String selectAllCardsByPage(Model model, int currentPage,  HttpSession session) {
        return cardService.selectAllCardsByPage(model, currentPage,  session);
    }
    /**
     * 鎵撳紑娣诲姞椤甸潰
     */
    @RequestMapping("/toAddCard")
    public String toAddCard(@ModelAttribute Card card) {
        return "addCard";
    }
    /**
     * 瀹炵幇娣诲姞鍙婁慨鏀瑰姛鑳�
     */
    @RequestMapping("/addCard")
    public String addCard(@ModelAttribute Card card, HttpServletRequest  request, String act, HttpSession session) throws IllegalStateException, IOException {
        return cardService.addCard(card, request, act, session);
    }
    /**
     * 鎵撳紑璇︽儏鍙婁慨鏀归〉闈�
     */
    @RequestMapping("/detail")
    public String detail(Model model, int id, String act) {

        return cardService.detail(model, id, act);
    }
    /**
     * 鍒犻櫎
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id) {
        return cardService.delete(id);
    }
    /**
     * 瀹夊叏閫�鍑�
     */
    @RequestMapping("/loginOut")
    public String loginOut(Model model, HttpSession session) {
        return cardService.loginOut(model, session);
    }
    /**
     * 鎵撳紑淇敼瀵嗙爜椤甸潰
     */
    @RequestMapping("/toUpdatePwd")
    public String toUpdatePwd(Model model, HttpSession session) {
        return cardService.toUpdatePwd(model, session);
    }
    /**
     * 淇敼瀵嗙爜
     */
    @RequestMapping("/updatePwd")
    public String updatePwd(@ModelAttribute MyUser myuser) {
        return cardService.updatePwd(myuser);
    }
}

